package com.woniuxy.main.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.session.Session;
import org.apache.shiro.web.session.HttpServletSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.main.pojo.Gym;
import com.woniuxy.main.pojo.Student;
import com.woniuxy.main.pojo.User;
import com.woniuxy.main.pojo.Vi_user_coach;
import com.woniuxy.main.pojo.Vi_user_gym;
import com.woniuxy.main.pojo.Vi_user_student;
import com.woniuxy.main.service.UserGymService;
import com.woniuxy.main.service.Vi_user_gymService;
import com.woniuxy.main.util.SessionUtil;

import lombok.Data;

@Controller
@RequestMapping("/vi_user_gym")
@Data
public class Vi_user_gymHandler {
	@Resource
	private Vi_user_gymService vi_user_gymService;
	@Resource
	private  UserGymService userGymService;
	
	@RequestMapping("/all")
	@ResponseBody
	public List<Vi_user_gym> all() {
		return vi_user_gymService.all();
	}
	
	@RequestMapping("/selectGymByUid")
	@ResponseBody
	public Vi_user_gym findGymByUid(int uid) {
		return vi_user_gymService.findGymByUid(uid);
		
	}
	
	@RequestMapping("/searchGym")
	@ResponseBody
	public List<Vi_user_gym> searchGym(String gname) {
		System.out.println(gname);
		if(gname != "") {
			gname = "%" + gname + "%";
		}
		return vi_user_gymService.searchGym(gname);
	}
	
	//分页查询所有场馆信息
	@RequestMapping("/showGymList")
	@ResponseBody
	public Map<String,Object> showCoachList(Integer page){
		//System.out.println("开始运行");
		int index = (page - 1) * 8;
		Map<String,Object> result=new HashMap<String,Object>();
		List<Vi_user_gym> info=vi_user_gymService.showGymList(index);
		result.put("form", info);
		//查询总页码
		int totalCount=vi_user_gymService.totalPage();
		int totalPage=totalCount % 8 == 0 ? totalCount / 8 : totalCount / 8 + 1;
		result.put("totalPage", totalPage);
		return result;
	}

   //查询场馆
	@RequestMapping("/selectByUid")
	@ResponseBody
	public Vi_user_gym selectByUid(HttpServletRequest request) {
		
		 /*根据session中保存的uid去查询当前用户详情*/
		
		int uid=(int)SessionUtil.getSession().getAttribute("uid");
		Vi_user_gym result = findGymByUid(uid);
		return result;
	}
	
	
	
	
	
	
	
	
	
	
	
	//更新场馆信息
	@RequestMapping("/updateGym")
	@ResponseBody
	public String updateGym(HttpServletRequest request,User user,Gym gym) {
		Session session = SessionUtil.getSession();
		int uid = (int) session.getAttribute("uid");
		// 模拟将session中的uid放入这个对象
		user.setUid(uid);
		gym.setUid(uid);

		// 将需要更新的字段写入数据库,返回总共影响行数
		int row =  userGymService.updateGym(user, gym);
		if (row != 0) {
			HttpSession session1 =request.getSession();
			session1.setAttribute("address", user.getAddress());
			session.setAttribute("address", user.getAddress());
			
			return "信息修改成功";
		}
		return "信息修改失败";

	}



}
